package com.sky.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 *  Redis 配置类
 */
@Slf4j
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    //自定义redisTemplate
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info(" 自定义redisTemplate初始化，并设置key-value的序列化...... ");
        //自定义redisTemplate 设置序列化
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();

        //默认使用的JdkSerializationRedisSerializer
        //string类型
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //hash类型
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
